package cn.ronghuanet.auth.controller;

import cn.ronghuanet.auth.annotation.RonghuaPermission;
import cn.ronghuanet.auth.domain.Role;
import cn.ronghuanet.auth.dto.RolePermissionDTO;
import cn.ronghuanet.auth.query.RoleQuery;
import cn.ronghuanet.auth.service.IRoleService;
import cn.ronghuanet.basic.utils.AjaxResult;
import cn.ronghuanet.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/role")
@RonghuaPermission(name = "角色管理")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @PutMapping
    @RonghuaPermission(name = "新增/修改")
    public AjaxResult addOrUpdate(@RequestBody Role role){
        try {
            if(Objects.isNull(role.getId())){
                roleService.add(role);
            }else{
                roleService.update(role);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败!"+e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "根据ID删除")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            roleService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    @PatchMapping
    @RonghuaPermission(name = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            roleService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败!"+e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @RonghuaPermission(name = "根据ID查询")
    public AjaxResult getById(@PathVariable("id")Long id){
        try {
            Role role = roleService.getById(id);
            return AjaxResult.me().setResultObj(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    @GetMapping
    @RonghuaPermission(name = "查询所有")
    public AjaxResult getAll(){
        try {
            List<Role> roles = roleService.getAll();
            return AjaxResult.me().setResultObj(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    @PostMapping
    @RonghuaPermission(name = "分页查询")
    public AjaxResult pageList(@RequestBody RoleQuery query){
        try {
            PageList<Role> rolePageList = roleService.pageList(query);
            return AjaxResult.me().setResultObj(rolePageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    @PutMapping("/permission")
    @RonghuaPermission(name = "设置权限")
    public AjaxResult setRolePermission(@RequestBody RolePermissionDTO dto){
        try {
            roleService.setRolePermission(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }

    @GetMapping("/permissionSns/{roleId}")
    @RonghuaPermission(name = "获取角色的权限标识")
    public AjaxResult getRolePermissionSns(@PathVariable("roleId")Long roleId){
        try {
            List<String> sns = roleService.getRolePermissionSns(roleId);
            return AjaxResult.me().setResultObj(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取角色的权限标识失败!"+e.getMessage());
        }
    }
}
